﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace uz.control
{
    [Serializable]
    public class WordIndex : SortedDictionary<string,WordIndex>
    {
        public bool IsWord { get; set; }

        public Dictionary<string, Word> WordInfos = new Dictionary<string, Word>();

        public void AddWord(Word word)
        {
            AddWord(word.ID);
            if (WordInfos.ContainsKey(word.ID) == false)
            {
                WordInfos.Add(word.ID, word);
            }
            else
            {
                if (WordInfos[word.ID].DisplayText != word.DisplayText)
                {
                    WordInfos[word.ID].Childs.Add(word);
                }
                else
                {
                    WordInfos[word.ID] = word;
                }
            }
        }

        /// <summary>
        /// 添加一个单词，并根据组成单词的字符创建树型索引
        /// </summary>
        /// <param name="Word">要添加的单词</param>
        private void AddWord(string Word)
        {
            string str = Word.Substring(0, 1);

            if (this.ContainsKey(str) == false)
            {
                this.Add(str, new WordIndex());
            }

            if (Word.Length == 1)
            {
                this[str].IsWord = true;
                return;
            }

            WordIndex wi = this[str];

            wi.AddWord(Word.Substring(1));
        }

        public List<string> GetWordIdList(string strSearch)
        {
            List<string> lstRst = new List<string>();

            if (this.IsWord == true)
            {
                lstRst.Add("");
            }

            if (this.Count == 0)
            {
                return lstRst;
            }

            if (strSearch.Length == 0)
            {
                foreach (string key in this.Keys)
                {
                    List<string> lst = this[key].GetWordIdList("");

                    foreach (string str in lst)
                    {
                        lstRst.Add(key + str);
                    }
                }
            }
            else
            {
                string str = strSearch.Substring(0, 1);
                if (this.ContainsKey(str) == true)
                {
                    List<string> lst = this[str].GetWordIdList(strSearch.Substring(1));

                    foreach (string itm in lst)
                    {
                        lstRst.Add(str + itm);
                    }
                }
            }

            return lstRst;
        }

        public List<Word> GetWordList(List<string> IdList)
        {
            List<Word> lstRst = new List<Word>();

            foreach (string id in IdList)
            {
                Word word = WordInfos[id];
                lstRst.Add(word);

                foreach (Word itm in word.Childs)
                {
                    lstRst.Add(itm);
                }
            }

            return lstRst;
        }
    }
}
